/*!
 * \file test_http.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */

#include <stdio.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <zlib.h>

#include "apmtest_cunit.h"
#include "frcomm.h"
#include "frmem.h"
#include "../../src/module/http/config.h"
#include "../../src/module/http/http_core.h"
#include "../../src/module/http/http1/http_analysis.h"
#include "../../src/module/http/http1/http_chunk.h"
#include "../../src/module/http/http2/nghttp2_header.h"
#include "../../src/module/http/http2/nghttp2_parse.h"
#include "t_comm/t_http_comm/t_http_comm.h"
#include "t_http1/t_http_analysis/t_http_analysis.h"
#include "t_http1/t_http_chunk/t_http_chunk.h"
#include <frservice_mng.h>

#include <pthread.h>
static Frmemstat_mng g_st_memstat_mng={0,FR_MEMSTAT_COUNT_MAX};
static pthread_rwlock_t  g_memstat_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static Frmem_env g_st_mem_env = {&g_st_memstat_mng, &g_memstat_rwlock};

void add_suites(FunType *p_func, int i_suites_len, FunType fp)
{
    int i_loop = 0;
    for (;i_loop < i_suites_len; i_loop++)
    {
        if (p_func[i_loop] == NULL)
        {
            p_func[i_loop] = fp;
            break;
        }
    }
}

int main(int argc, char **argv)
{
    if (fr_slab_init(&g_st_mem_env, MODULE_NAME) == NULL)
        return FR_FAIL;

    apmtest_cunit_registry();

    FunType fun_suites[1024];
    memset(fun_suites, 0x00, sizeof(fun_suites));
    int i_suites_len = sizeof(fun_suites) / sizeof(FunType);
    int i_loop = 0;

    // 加载各个测试组件
    // t_comm/http_comm
    add_suites(fun_suites, i_suites_len, add_suite_http_auth_parse);
    add_suites(fun_suites, i_suites_len, add_suite_http_url_decode);
    add_suites(fun_suites, i_suites_len, add_suite_multipart_data_end);
    add_suites(fun_suites, i_suites_len, add_suite_multipart_data_parse);
    add_suites(fun_suites, i_suites_len, add_suite_get_req_data);
    add_suites(fun_suites, i_suites_len, add_suite_get_resp_data);

    //t_http1/t_http_analysis
    add_suites(fun_suites, i_suites_len, add_suite___http_req_header_detect);
    add_suites(fun_suites, i_suites_len, add_suite___http_get_req_inf);
    add_suites(fun_suites, i_suites_len, add_suite_http_get_whole_req_inf);
    add_suites(fun_suites, i_suites_len, add_suite___http_resp_header_detect);
    add_suites(fun_suites, i_suites_len, add_suite___http_get_resp_inf);
    add_suites(fun_suites, i_suites_len, add_suite_http_get_whole_resp_inf);

    //t_http1/t_http_chunk
    add_suites(fun_suites, i_suites_len, add_suite_de_chunked);

    for (;i_loop < i_suites_len; i_loop++)
    {
        if (fun_suites[i_loop] != NULL && fun_suites[i_loop]() != 0)
        {
            CU_cleanup_registry();
            return CU_get_error();
        }
    }

    apmtest_cunit_unregistry();
    fr_slab_destroy();

    return 0;
}
